package com.bwie.system.config;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName:
 * @Description:  消息发送到 broker 的回调 处理类 【确认】
 * @Author: Kouwenzhuo
 * @Date: 2024/4/5
 */
@Component
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * ConfirmCallbackConfig 在初始化的时候执行的方法
     */
    @PostConstruct
    public void init() {
        this.rabbitTemplate.setConfirmCallback(this);
    }

    /**
     * 回调方法 消息发送到 broker 无论成功还是失败都是执行这个 方法
     * @param correlationData correlation data for the callback.
     * @param ack true for ack, false for nack
     * @param cause An optional cause, for nack, when available, otherwise null.
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            System.out.println("消息发送到broker成功");
        } else {
            System.out.println("消息发送到broker失败，失败的原因：" + cause);
        }
    }

}
